<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Windows Live Test</title>
    <style>
        <!--
        .Name {
            font-family: 'Segoe UI', Verdana, Tahoma, Helvetica, Arial, sans-serif;
            font-weight: bold;
        }

        -->
    </style>
</head>
<body>

<?php 
$client_id = "kpvcrh7lhxikf8vl95a0uwa6uq97t9sq";
	session_start();

$loginUrl = sprintf("https://api.box.com/oauth2/authorize?response_type=code&client_id=%s&state=authenticated", $client_id);

?>
<a href="<?php echo $loginUrl ?>">Login to your Box Account</a>

<h1>Windows Live Test</h1>

<div>
    <div id="meName" class="Name"></div>
    <div id="meImg"></div>
    <div id="signin"></div>
    <br/>
    <br/>
    <br/>
    <div id="meQuota"></div>
    <div id="meFiles"></div>
    <div id="uploadFile">
		<form>
	    	<input id="file" name="file" type="file" />
		</form>    	
    	<button onclick="uploadFile_fileDialog()">Save file with SkyDrive file picker (calling WL.filedialog)</button>

		
    </div>
   
    
</div>
<script src="//js.live.net/v5.0/wl.js" type="text/javascript"></script>
<script type="text/javascript">

    // Update the following values
    var client_id = "000000004C0DFC8A",
        scope = ["wl.signin", "wl.basic", "wl.offline_access", "wl.skydrive", "wl.skydrive_update"],
        redirect_uri = "https://thegodzilladrive.com/pennapps/callback.php";

    function id(domId) {
        return document.getElementById(domId);
    }

    function displayMe() {
        var imgHolder = id("meImg"),
            nameHolder = id("meName"),
			quotaHolder = id("meQuota"),
			filesHolder = id("meFiles"),
			uploadHolder = id("uploadFile");
			
        if (imgHolder.innerHTML != "") return;

        if (WL.getSession() != null) {
            WL.api({ path: "me/picture", method: "get" }).then(
                    function (response) {
                        if (response.location) {
                            imgHolder.innerHTML = "<img src='" + response.location + "' />";
                        }
                    }
                );

            WL.api({ path: "me", method: "get" }).then(
                    function (response) {
                        nameHolder.innerHTML = response.name;
                    }
                );

            WL.api({ path: "/me/skydrive/quota", method: "GET" }).then(
                    function(response) {
                        quotaHolder.innerHTML = (JSON.stringify(response).replace(/,/g, ",\n"));
                    },
                    function(response) {
                    	quotaHolder.innerHTML = ("Could not access quota, status = " +
                            JSON.stringify(response.error).replace(/,/g, ",\n"));
                    }
                );

            WL.api({ path: "/me/skydrive/my_documents", method: "GET" }).then(
                    function(response) {
                        filesHolder.innerHTML = (JSON.stringify(response).replace(/,/g, ",\n"));
                    },
                    function(response) {
                    	filesHolder.innerHTML = ("Could not access drive, status = " +
                            JSON.stringify(response.error).replace(/,/g, ",\n"));
                    }
                );
        }
    }

    function clearMe() {
        id("meImg").innerHTML = "";
        id("meName").innerHTML = "";
    }

    WL.Event.subscribe("auth.sessionChange",
        function (e) {
            if (e.session) {
                displayMe();
            }
            else {
                clearMe();
            }            
        }
    );

    function uploadFile_fileDialog() {
        WL.fileDialog({
            mode: "save"
        }).then(
            function (response) {
                WL.upload({
                    path: response.data.folders[0].id,
                    element: "file",
                    overwrite: "rename"
                }).then(
                    function (response) {
                        document.getElementById("info").innerText =
                            "File uploaded.";
                    },
                    function (responseFailed) {
                        document.getElementById("info").innerText =
                            "Error uploading file: " + responseFailed.error.message;
                    }
                );
            },
            function (responseFailed) {
                document.getElementById("info").innerText =
                    "Error getting folder info: " + responseFailed.error.message;
            }
        );  
    }
    
    WL.init({ client_id: client_id, redirect_uri: redirect_uri, response_type: "code", scope: scope });

    WL.ui({ name: "signin", element: "signin" });

    
</script>

<?php 
	
	if(isset($_GET['access_token']))
	{
		$baseURL = "https://api.box.com/2.0";
		
		function get($url)
		{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$_GET['access_token']));
			
			$data = curl_exec($ch);
			curl_close($ch);
		
			return json_decode($data);
		}	
		
		$boxUserData = get($baseURL."/users/me");
		
		echo "Box User Space = ".$boxUserData->space_amount;
		echo "<br/>";
		echo "Box User Used Space = ".$boxUserData->space_used;
		echo "<br/>";
		echo "Box Max Upload Size = ".$boxUserData->max_upload_size;
		
		function getFolderItems($id, $baseURL)
		{
			$boxFileData = get($baseURL."/folders/".$id."/items");
			echo "<br/>";
			echo "Box File Information ";
			
			//print_r($boxFileData);
			
			//$entries = $boxFileData->entries;
			
			for ($i=0; $i<$boxFileData->total_count; $i++)
			{
				$file = $boxFileData->entries[$i];
				print_r($file->name); 
				$boxFileInfo = get($baseURL."/files/".$file->id);
				echo "<br/>";	
				echo "Modified at ";
				print_r($boxFileInfo->modified_at);
			}
			
		}
		
		
		getFolderItems(0, $baseURL);
		
	}
	
	
	
?>

<form action="https://thegodzilladrive.com/pennapps/uploadBox.php" method="post" enctype="multipart/form-data">

	   <input id="access_token" name="access_token" type="hidden" value="<?php if(isset($_GET['access_token'])){echo $_GET['access_token'];}?>"/>
	   <input id="file2" name="file2" type="file" />
	   <input type="submit" value="Save file with Box">
</form>

</body>
</html>